package com.houdoudou.affair.jsp;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houdoudou.bean.Box;
import com.houdoudou.bean.Machine;
import com.houdoudou.bean.Owner;
import com.houdoudou.bean.Programmer;
import com.houdoudou.dao.BoxDao;
import com.houdoudou.dao.ConnFactory;
import com.houdoudou.dao.MachineDao;
import com.houdoudou.dao.OwnerDao;
import com.houdoudou.dao.ProgrammerDao;

public class ProgrammerAffairJsp1 {

/*------------------------------------------新增业主-------------------------------------------------*/	
	
	public static void createOwner(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		Owner owner = null;
		//获取Bean---------------------------
		 owner= Owner.beanFromRequest(req);
		try{
		 Owner.checkValid(owner);
		 owner= Owner.encrypt(owner);
		 }catch(Exception e2){
			 e2.printStackTrace();
			    success=false;
			    errorInfo="传入参数有误"+e2.getMessage();
		 }
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   OwnerDao ownerDao= new  OwnerDao(conn);
		   Owner ownerExist= ownerDao.selectByTelnum(owner.getTelnum());
		   if(!ownerExist.getTelnum().isEmpty()){
			  success=false;
			   errorInfo="该用户已经存在";
		   }else{
			   ownerDao.create(owner); 
			   success=true;
			   errorInfo="创建成功";
		   }
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
			
			 //处理事务结果跳转------------------
			 String jumpJspPath=req.getContextPath()+"/normalProgrammer/createOwner.jsp";
				Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
			 
			 
	}
	
	
/*------------------------------------------搜索业主-------------------------------------------------*/	
	
	public static void searchOwner(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo=null;
		//获取Bean---------------------------		
		Owner owner= new Owner();	
		owner.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		 List<Owner> owners = new ArrayList<Owner>();
		System.out.println(""+owner.getSecret()+owner.getTelnum());
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   OwnerDao ownerDao= new  OwnerDao(conn);
		   
		   if(owner.getTelnum().isEmpty()){
			owners= ownerDao.selectAll();
			success=true;
			   
		   }else{
			  Owner temp= ownerDao.selectByTelnum(owner.getTelnum());
			  owners.add(temp);
			  success=true;
			  
		   }
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
			
				 req.setAttribute("success", true);	
				 req.setAttribute("errorInfo", errorInfo);		
			      if(success){
			    	  req.setAttribute("owners", owners);	 
			      }
			 //处理事务结果跳转------------------
			      String jumpJspPath="/normalProgrammer/manageOwner.jsp";
					Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
			 
			 
	}
	
/*------------------------------------------删除业主-------------------------------------------------*/	
	
	public static void deleteOwner(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------		
		Owner owner= new Owner();	
		owner.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   OwnerDao ownerDao= new  OwnerDao(conn);
		   ownerDao.delete(owner.getTelnum());
		   success=true;
		   errorInfo="删除业主"+owner.getTelnum()+"成功,已刷新界面，请重新搜索一遍";
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------	
			 //处理事务结果跳转------------------
				 String jumpJspPath=req.getContextPath()+"/normalProgrammer/manageOwner.jsp";
				 Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
			 
	}

/*------------------------------------------修改业主-------------------------------------------------*/	
	
	public static void modifyOwner(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo = null;
		 Owner temp = null;
		//获取Bean---------------------------		
		Owner owner= new Owner();	
		owner.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		 List<Owner> owners = new ArrayList<Owner>();
		System.out.println(""+owner.getSecret()+owner.getTelnum());
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   OwnerDao ownerDao= new  OwnerDao(conn);
		   
		 
			  temp= ownerDao.selectByTelnum(owner.getTelnum());
			  owners.add(temp);
			
		 
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
			
				
			    	  req.setAttribute("owner", temp);	 
			  
			 //处理事务结果跳转------------------
			      String jumpJspPath="/normalProgrammer/modifyOwner.jsp";
					Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
			 
			 
	}
/*------------------------------------------更新业主-------------------------------------------------*/	
	
	public static void updateOwner(HttpServletRequest req, HttpServletResponse resp){		
		boolean success=true;
		String  errorInfo="未执行任何操作";
		 String jumpJspPath=req.getContextPath()+"/normalProgrammer/modifyOwner.jsp";	
		//获取Bean---------------------------
		 Owner owner = null;
		 owner = Owner.beanFromRequest(req);
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   OwnerDao ownerDao= new  OwnerDao(conn);
		   Owner ownerExist= ownerDao.selectByTelnum(owner.getTelnum());
		   ownerExist= Owner.decrypt(ownerExist);
		   owner= Owner.merge(ownerExist, owner);
		   Owner.checkValid(owner);
		   owner=Owner.encrypt(owner);
		   int acount =ownerDao.update(owner);
		   if(acount>0) {
			success=true;
			errorInfo="修改成功";  
		   }else{
			   success=true;
			   errorInfo="修改失败"; 
		   }
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			   success=false;
			   errorInfo="数据库异常"+e.getMessage();
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
		// dao处理结束
		
	//处理事务结果，赋值----------------------
				      
   //处理事务结果跳转------------------
		 Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
   
	}
	
/*------------------------------------------新增自提柜-------------------------------------------------*/	
	
	public static void createMachine(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		 String jumpJspPath=req.getContextPath()+"/normalProgrammer/createMachine.jsp"; 
		Machine machine = null;
		//获取Bean---------------------------
		try{
		 machine= Machine.beanFromRequest(req);
		
		 }catch(Exception e2){
			 e2.printStackTrace();		 
			    success=false;
			    errorInfo="传入参数有误"+e2.getMessage();
			//    Common.forwardToJsp(req,resp,"/normalProgrammer/createMachine.jsp",errorInfo);
			    return ;
		 }
		//获取Bean---------------------------
				try{
				Machine.checkMachineValid(machine);
				 machine= Machine.encrypt(machine);
				 }catch(Exception e2){
					 e2.printStackTrace();
					 
					    success=false;
					    errorInfo="传入参数有误"+e2.getMessage();
					//    Common.forwardToJsp(req,resp,"/normalProgrammer/createMachine.jsp",errorInfo);
					    return ;
				 }
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   MachineDao machineDao= new  MachineDao(conn);	  
			 int id=  machineDao.create(machine); 
			 BoxDao boxDao = new BoxDao(conn);
			 Box box = new Box();
			 box.setMachine(id);
			 box.setState(1);//表示空闲			 
			 for(int i=1;i<machine.getBoxNum();i++){
				 box.setId(i);
				 boxDao.create(box);
			 }
			   success=true;
			   errorInfo="创建自提柜成功,编号："+id;
			   Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
			//   Common.forwardToJsp(req,resp,"/normalProgrammer/createMachine.jsp",errorInfo);
		}finally{
			 ConnFactory.closeConn(conn);	
				
		}
				 
	}

	
	
/*------------------------------------------搜索自提柜-------------------------------------------------*/	
	
	public static void searchMachine(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo=null;
		//获取Bean---------------------------		
		Machine machine;
		try {
			machine = Machine.beanFromRequest(req);
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			 Common.forwardToJsp(req,resp,"/normalProgrammer/manageMachine.jsp","传入参数有误"+e2.getMessage());
			 return;
		}
	//	machine.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		 List<Machine> machines = new ArrayList<Machine>();
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();  
		   MachineDao machineDao= new  MachineDao(conn);
		   String sql=Machine.getSearchSql(machine);
		   System.out.println(sql);
		   machines= machineDao.search(sql);	  		   
		   success=true;
		}catch(Exception e){
			e.printStackTrace();
			 success=false;
			  errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
		   req.setAttribute("success", success);	
           if(success){		   req.getSession().setAttribute("machines", machines);	}
		   Common.forwardToJsp(req,resp,"/normalProgrammer/manageMachine.jsp",errorInfo);
			  
					
			 
			 
	}

	
	
/*------------------------------------------修改自提柜-------------------------------------------------*/	
	
	public static void modifyMachine(HttpServletRequest req, HttpServletResponse resp){		
		
		
		String  errorInfo=null;
		Machine machine = null;
		//获取Bean---------------------------
		try{
		 machine= Machine.beanFromRequest(req);
		 if(machine.getId()==0) { errorInfo="请传入参数：编号";
		    Common.forwardToJsp(req,resp,"/normalProgrammer/modifyMachine.jsp",errorInfo);}
		 }catch(Exception e2){
			 e2.printStackTrace();		 
			
			    errorInfo="传入参数有误"+e2.getMessage();
			    Common.forwardToJsp(req,resp,"/normalProgrammer/modifyMachine.jsp",errorInfo);
			    return ;
		 }
	
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   MachineDao machineDao= new  MachineDao(conn);	  
		   machine=  machineDao.selectById(machine.getId());
			
		  
			   req.setAttribute("success", true);
			   req.setAttribute("machine", machine);
			  
			   Common.forwardToJsp(req,resp,"/normalProgrammer/modifyMachine.jsp",null);
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			   errorInfo="数据库异常";
		    Common.forwardToJsp(req,resp,"/normalProgrammer/modifyMachine.jsp",errorInfo);
		}finally{
			 ConnFactory.closeConn(conn);	
				
		}
				 
	}
/*------------------------------------------查看自提柜-------------------------------------------------*/	
	
	public static void detailMachine(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo=null;
		Machine machine = null;
		//获取Bean---------------------------
		try{
		 machine= Machine.beanFromRequest(req);
		 if(machine.getId()==0) { errorInfo="请传入参数：编号";
		    Common.forwardToJsp(req,resp,"/normalProgrammer/detailMachine.jsp",errorInfo);}
		 }catch(Exception e2){
			 e2.printStackTrace();		 
			    success=false;
			    errorInfo="传入参数有误"+e2.getMessage();
			    Common.forwardToJsp(req,resp,"/normalProgrammer/detailMachine.jsp",errorInfo);
			    return ;
		 }
	
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   MachineDao machineDao= new  MachineDao(conn);	  
			 machine=  machineDao.selectById(machine.getId());
			   success=true;
			   req.setAttribute("success", true);
			 req.setAttribute("machine", machine);
			   Common.forwardToJsp(req,resp,"/normalProgrammer/detailMachine.jsp",null);
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		    Common.forwardToJsp(req,resp,"/normalProgrammer/detailMachine.jsp",errorInfo);
		}finally{
			 ConnFactory.closeConn(conn);	
				
		}
				 
	}
/*------------------------------------------更新自提柜-------------------------------------------------*/	
	
	public static void updateMachine(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo=null;
		Machine machine = null;
		//获取Bean---------------------------
		try{
		 machine= Machine.beanFromRequest(req);
	//	 machine= Machine.encrypt(machine);
	//	Machine machineInReq= (Machine) req.getAttribute("machine");
	//	Machine.printMachine(machineInReq);
		 
		 }catch(Exception e2){
			 e2.printStackTrace();		 
			    success=false;
			    errorInfo="传入参数有误"+e2.getMessage();
				req.setAttribute("machine", machine);
			    Common.forwardToJsp(req,resp,"/normalProgrammer/modifyMachine.jsp",errorInfo);
			    return ;
		 }		
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		Machine machineExist = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   MachineDao machineDao= new  MachineDao(conn);	  
			 machineExist=  machineDao.selectById(machine.getId());
	    	 machineExist= Machine.decrypt(machineExist);
			machine= Machine.merge(machineExist, machine);
			
			try{Machine.checkMachineValid(machine);
			machine= Machine.encrypt(machine);
			}catch(Exception e){
				req.setAttribute("machine", machine);
				 Common.forwardToJsp(req,resp,"/normalProgrammer/modifyMachine.jsp",e.getMessage());
				 return;
			}
			machineDao.update(machine);	
			conn.commit();
			   success=true;
			   req.setAttribute("success", true);
			   req.setAttribute("machine", machine);
			   Common.forwardToJsp(req,resp,"/normalProgrammer/modifyMachine.jsp","修改成功");
		   
		}catch(Exception e){e.printStackTrace();
			try {conn.rollback();} catch (SQLException e1) {	e1.printStackTrace();}
			 success=false;
			   errorInfo="数据库异常";
			   req.setAttribute("machine", machine);
		    Common.forwardToJsp(req,resp,"/normalProgrammer/modifyMachine.jsp",errorInfo);
		}finally{
			 ConnFactory.closeConn(conn);	
				
		}
				 
	}
/*------------------------------------------删除自提柜-------------------------------------------------*/	
	
	public static void deleteMachine(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------		
		int id= Integer.parseInt(req.getParameter("id"));
		// 初始化搜索结果
		
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   MachineDao machineDao= new  MachineDao(conn);
		   machineDao.delete(id);
		   success=true;
		  List<Machine> machines= (List<Machine>) req.getSession().getAttribute("machines");
		  for(int i=machines.size()-1;i>=0;i--){
			  if(machines.get(i).getId()==id){ machines.remove(i); break;}
		  }
		  req.getSession().setAttribute("machines", machines);
		  req.setAttribute("success", true);
		   errorInfo="删除"+id+"自提柜成功";
		   Common.forwardToJsp(req,resp,"/normalProgrammer/manageMachine.jsp",errorInfo);
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}			 
	}
	
	/*------------------------------------------增补创建箱格-------------------------------------------------*/	
	
	  public static void addCreateBox(HttpServletRequest req, HttpServletResponse resp){	
		  
			String  errorInfo=null;
			Machine machine = null;
			String jumpJspPath="/programmer/basicResult.jsp";
			//获取Bean---------------------------
			try{
			 machine= Machine.beanFromRequest(req);
			 if(machine.getId()==0) { errorInfo="请传入参数：编号";
			    Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);}
			 }catch(Exception e2){
				 e2.printStackTrace();		 
				
				    errorInfo="传入参数有误"+e2.getMessage();
				    Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
				    return ;
			 }
		
			//调用 dao处理事务 -------------------------
			Connection conn = null;
			try{
			   conn= ConnFactory.getConn();
			   conn.setAutoCommit(false);
			   BoxDao boxDao = new BoxDao(conn);
				 Box box = new Box();
				 box.setMachine(machine.getId());
				 box.setState(1);//表示空闲			 
				 for(int i=1;i<=machine.getBoxNum();i++){
					 box.setId(i);
					 boxDao.create(box);
				 }			  
				   Common.forwardToJsp(req,resp,jumpJspPath,"箱格增补成功");
			   conn.commit();
			}catch(Exception e){
				e.printStackTrace();
				try {	conn.rollback();} catch (SQLException e1) {e1.printStackTrace();	}
				
				   errorInfo="数据库异常";
			    Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
			}finally{
				 ConnFactory.closeConn(conn);	
					
			}
		  
		}
	  /*------------------------------------------管理箱格-------------------------------------------------*/	
		
	  public static void searchBox(HttpServletRequest req, HttpServletResponse resp){	
		  
			String  errorInfo=null;
			Box box = null;
			String jumpJspPath="/normalProgrammer/manageBox.jsp";
			//获取Bean---------------------------
			try{
			 box= Box.beanFromRequest(req);	    
			 }catch(Exception e2){
				 e2.printStackTrace();				
				    errorInfo="传入参数有误"+e2.getMessage();
				    Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
				    return ;
			 }
		
			//调用 dao处理事务 -------------------------
			Connection conn = null;
			try{
			   conn= ConnFactory.getConn();
			   BoxDao boxDao= new BoxDao(conn);	    
			   List<Box> boxes= boxDao.search(BoxDao.getSearchSql(box));
			   req.getSession().setAttribute("boxes", boxes);	
			   req.setAttribute("success", true);
			  Common.forwardToJsp(req,resp,jumpJspPath);
			 
			   return;
			}catch(Exception e){
				e.printStackTrace();	
				   errorInfo="数据库异常";
			    Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
			    return;
			}finally{
				 ConnFactory.closeConn(conn);	
					
			}
		  
		}
	  /*------------------------------------------更新箱格-------------------------------------------------*/	
		
	  public static void updateBox(HttpServletRequest req, HttpServletResponse resp){	
		  
			String  errorInfo=null;
			Box box = null;
			String jumpJspPath="/normalProgrammer/modifyBox.jsp";
			//获取Bean---------------------------
			try{
			 box= Box.beanFromRequest(req);	    
			 }catch(Exception e2){
				 e2.printStackTrace();				
				    errorInfo="传入参数有误"+e2.getMessage();
				    Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
				    return ;
			 }
		
			//调用 dao处理事务 -------------------------
			Connection conn = null;
			try{
			   conn= ConnFactory.getConn();
			   BoxDao boxDao= new BoxDao(conn);	    
			   boxDao.update(box);
			  
			  Common.forwardToJsp(req,resp,jumpJspPath,"修改成功");
			 
			   return;
			}catch(Exception e){
				e.printStackTrace();	
				   errorInfo="数据库异常";
			    Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
			    return;
			}finally{
				 ConnFactory.closeConn(conn);	
					
			}
		  
		}  
}
